/**
* 版权声明：${codeGenConfig.developerConfig.company!} 版权所有 违者必究
* 日    期：${date!}
*/
package ${codeGenConfig.table.packageVar!}.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import ${codeGenConfig.table.entityPackageVar!}.entity.${codeGenConfig.table.classVar!};
import ${codeGenConfig.table.packageVar!}.service.${codeGenConfig.table.classVar!}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * ${codeGenConfig.table.tableName!}控制层
 *
 * @author ${codeGenConfig.developerConfig.developer!}
 * @version ${codeGenConfig.developerConfig.version!}
 * @date ${date!}
 */
@Api(description = "${codeGenConfig.table.tableName!}接口")
@RestController
@RequestMapping("/admin/${codeGenConfig.table.simplePackageVar!}")
public class ${codeGenConfig.table.classVar!}Controller extends BaseController {

    @Autowired
    private ${codeGenConfig.table.classVar!}Service ${codeGenConfig.table.classVariable!}Service;

    /**
     * 分页查询${codeGenConfig.table.tableName!}
     *
     * @return
    */
    @ApiOperation("分页查询${codeGenConfig.table.tableName!}")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query")
    })
    public CommResult<${codeGenConfig.table.classVar!}> list(Date startDate, Date endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<${codeGenConfig.table.classVar!}> list = ${codeGenConfig.table.classVariable!}Service.selectList(params);
        PageResult<${codeGenConfig.table.classVar!}> pageResult = new PageResult<${codeGenConfig.table.classVar!}>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条${codeGenConfig.table.tableName!}
     *
     * @return
    */
    @ApiOperation("查询单条${codeGenConfig.table.tableName!}")
    @GetMapping("/find/{id}")
    public CommResult<${codeGenConfig.table.classVar!}> find(@PathVariable String id) {
        ${codeGenConfig.table.classVar!} result = ${codeGenConfig.table.classVariable!}Service.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("${codeGenConfig.table.classVariable!}",result);
        return CommResult.success(data);

    }

    /**
     * 保存${codeGenConfig.table.tableName!}
     *
     * @param ${codeGenConfig.table.classVariable!}
     * @return
     */
    @ApiOperation("保存${codeGenConfig.table.tableName!}")
    @PostMapping("/save")
    public CommResult<${codeGenConfig.table.classVar!}> save(@RequestBody ${codeGenConfig.table.classVar!} ${codeGenConfig.table.classVariable!}) {
        int affectCount = ${codeGenConfig.table.classVariable!}Service.insert(${codeGenConfig.table.classVariable!});
        if (affectCount <= 0) {
            return CommResult.error();
        }
        ${codeGenConfig.table.classVar!} result = ${codeGenConfig.table.classVariable!}Service.findById(${codeGenConfig.table.classVariable!}.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除${codeGenConfig.table.tableName!}
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除${codeGenConfig.table.tableName!}", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除${codeGenConfig.table.tableName!},ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<${codeGenConfig.table.classVar!}> del(@PathVariable String ids) {

        ${codeGenConfig.table.classVariable!}Service.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新${codeGenConfig.table.tableName!}
     *
     * @param ${codeGenConfig.table.classVariable!}
     * @return
     */
    @Log(desc = "更新${codeGenConfig.table.tableName!}", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新${codeGenConfig.table.tableName!}")
    @PostMapping("/update")
    public CommResult<${codeGenConfig.table.classVar!}> update(@RequestBody ${codeGenConfig.table.classVar!} ${codeGenConfig.table.classVariable!}) {
        int affectCount = ${codeGenConfig.table.classVariable!}Service.updateByPrimaryKeySelective(${codeGenConfig.table.classVariable!});
        if (affectCount <= 0){
            return CommResult.error();
        }
        ${codeGenConfig.table.classVar!} result = ${codeGenConfig.table.classVariable!}Service.findById(${codeGenConfig.table.classVariable!}.getId());
        return CommResult.success(result);
    }

}
